// 校验工具类
function Validate(aString, regex) {
  var validRegEx = new RegExp(regex);

  if (!validRegEx.test(aString + "")) {
    return false;
  }

  return true;
}




//银行卡校验
function verifyBankCard(num) {
  num = num.toString().split('');//变为字符串再转数组
  if(num.length > 19 || num.length < 15) return false;//银行卡号长度为15-19
  let lastNum = parseInt(num[num.length-1]);//取出最后一位--校验位
  num.length = num.length - 1;//删除最后一个值
  num = num.reverse();//变为数组再颠倒

  let sum = 0;//总数
  num.forEach((elem,i) => {
    elem = parseInt(elem);
    if(i % 2 === 0) {
      let n = elem * 2;
      if(n < 10) sum += n;
      else sum += (parseInt(n/10) + n%10);
    }
    else sum += elem;
  });
  return (sum+lastNum) % 10 === 0 ? true : false;
  
}



/**
 *  限制位数
 *  @param limitLength 位数
 */


function isLength(aString, limitLength) {
  return aString.length <= limitLength;
}

function isNumber(aString, decimalLimit) {
  //    NSString *validRegEx =@"^[0-9]*$";
  var dotCount = 0;

  if (decimalLimit > 0) {
    dotCount = 1;
  }

  var regex = "^[0-9]+(\\.{0," + dotCount + "}[0-9]{0," + decimalLimit + "})?$";
  return Validate(aString, regex);
} // /**
//  *  是否是指定长度数字
//  *  @param decimalLimit 小数位数
//  *  @param integerMaxLength 整数最大位数
//  */
// function isNumber(aString,decimalLimit,integerMaxLength)
// {
//   var dotCount = 0;
//   if (decimalLimit > 0) {
//     dotCount = 1;
//   }
//   var reg = "^[0-9]{0," + integerMaxLength + "}+(\\.{0," + dotCount + "}[0-9]{0," + decimalLimit + "})?$";
//  
//   return Validate(aString, reg);
// }

/**
 *  是否是数字或字母
 *  @param length 长度
 */


function isNumberOrAlphabet(aString, maxLength) {
  var regex = "^[0-9A-Za-z]{0," + maxLength + "}";
  return Validate(aString, regex);
} //电话号 简单校验


function isTelphone(aString) {
  var regex = "^(0[0-9]{2,3}\\-)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?$";
  return Validate(aString, regex);
} //手机号 校验


function isMobile(aString) {
  // var regex = "^(0|86|17951)?(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57]|19[9])[0-9]{8}$";
  // return Validate(aString, regex);
  
  if(aString && aString.length==11 && aString.startsWith("1")){
	  return true;
  }else{
	  return false;
  }
} //车牌号验证


function isCarNumber(carNo) {
  //    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
  //    NSString *carRegex = @"^[\u4e00-\u9fff]{1}[a-zA-Z]{1}[-][a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fff]$";//其中\u4e00-\u9fa5表示unicode编码中汉字已编码部分，\u9fa5-\u9fff是保留部分，将来可能会添加
  //    if ([carNo hasPrefix:@"w"] || [carNo hasPrefix:@"W"]) {
  //        NSString *wj =   @"^(w|W){0,1}(j|J){0,1}[a-zA-Z_0-9]*$";
  //        return [[self class] Validate:carNo regex:wj];
  //    }
  var carRegexDCARS = "^[\u4e00-\u9fff]{1}[a-zA-Z_0-9]*";
  return Validate(aString, carRegexDCARS);
} //经销商代码验证


function isSACode(SACode) {
  var saRegex = "^(SA|sa|sA|SA)([0-9]{5})$";
  return Validate(aString, saRegex);
} //车架号简单验证


function isVINNumber(vinNO, strictMode) {
  if (strictMode) {}

  var regex = "^[0-9A-Za-z]{17}";
  return Validate(aString, regex);
} //邮箱


function isEmail(aString) {
  var regex = "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  return Validate(aString, regex);
} //身份证号 简单验证


function isIDCardFormat(aString) {
  var regex = "^(\\d{14}|\\d{17})(\\d|[xX])$";
  return Validate(aString, regex);
} //ip 地址


function isIPAddress(aString) {
  var regex = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
  var result = Validate(aString, regex);

  if (result) {
    var componds = aString.split('.');
    var v = true;

    for (var s in componds) {
      if (componds[s] > 255) {
        v = NO;
        break;
      }
    }

    return v;
  }

  return false;
} //mac 地址


function isMacAddress(aString) {
  var macAddRegex = "([A-Fa-f\\d]{2}:){5}[A-Fa-f\\d]{2}";
  return Validate(aString, macAddRegex);
} //网址


function isUrl(aString) {
  var macAddRegex = "^((http)|(https))+:[^\\s]+\\.[^\\s]*$";
  return Validate(aString, macAddRegex);
} //中文


function isChinese(aString) {
  var macAddRegex = "^[\u4e00-\u9fa5]+$";
  return Validate(aString, macAddRegex);
} //邮编


function isPostCode(aString) {
  var macAddRegex = "^[0-8]\\d{5}(?!\\d)$";
  return Validate(aString, macAddRegex);
}
/**
 * 根据身份证获取年龄、性别等数据,sex : 0 表示女 1 表示男
 */


function getIdCardInfo(idcard) {
  if (idcard == null || idcard.length != 18) return null;
  let age = 151;
  let sex = 0; // 0 表示女 1 表示男

  let str = idcard;
  let year = Number(str.slice(6, 10));
  let month = Number(str.slice(10, 12));
  let day = Number(str.slice(12, 14));
  let date = new Date();
  let year2 = date.getFullYear();
  let sexValue = Number(str.slice(16, 17));
  age = year2 - year;
  return {
    'age': age,
    'sex': sexValue % 2,
    // 0 表示女 1 表示男
    'year': year,
    // 出生年
    'month': month,
    // 出生月
    'day': day // 出生日

  };
}

module.exports = {
  Validate: Validate,
  isNumber: isNumber,
  isLength: isLength,
  isNumberOrAlphabet: isNumberOrAlphabet,
  isTelphone: isTelphone,
  isMobile: isMobile,
  isCarNumber: isCarNumber,
  isSACode: isSACode,
  isVINNumber: isVINNumber,
  isEmail: isEmail,
  isIDCardFormat: isIDCardFormat,
  isIPAddress: isIPAddress,
  isMacAddress: isMacAddress,
  isUrl: isUrl,
  isChinese: isChinese,
  isPostCode: isPostCode,
  getIdCardInfo: getIdCardInfo,
  verifyBankCard:verifyBankCard
};